package com.zc.im.register.config;

import com.zc.im.common.config.ImConfigProperties;
import com.zc.im.register.loadbanacer.ConnectLoadBalancer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.util.ServiceLoader;

/**
 * Project：im
 * Date：2022/4/27
 * Time：16:39
 * Description：连接器注册中心负载均衡器注入
 *
 * @author Challen.Zhang
 * @version 1.0
 */
public class RegisterLoadBalancerSelector implements ImportSelector {

    @Autowired
    private ImConfigProperties imConfigProperties;

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        ServiceLoader<ConnectLoadBalancer> load = ServiceLoader.load(ConnectLoadBalancer.class);
        for (ConnectLoadBalancer connectLoadBalancer : load) {
            if (connectLoadBalancer.getType().equals(imConfigProperties.getRegisterConfig().getType())) {
                return new String[]{connectLoadBalancer.getClass().getName()};
            }
        }
        return new String[0];
    }
}
